﻿#include <QtGui>
#include <QTCore>

#include "MainFrm.h"
#include "FormNote.h"
#include "FormDiary.h"
#include "FormContact.h"
#include "FormAccount.h"
#include "TestDialog.h"
#include "WidgetGroup.h"

MainFrame::MainFrame():
	m_pWidgetGroup(new WidgetGroup(this, tr("Personal Assistant")))
{
	setWindowIcon(QIcon(tr("./images/agent.png")));
	createAction();
	
	FillWidgetGroup();
}

MainFrame::~MainFrame()
{

}

void MainFrame::FillWidgetGroup()
{
	QStackedWidget *stack = m_pWidgetGroup->StackedWidget();

	SubFrame* wid = new FormDiary(stack);
	m_pWidgetGroup->AddChild(tr("./images/diary.png"), tr("Diary"), wid);
	m_pWidgetGroup->AddChild(tr("./images/note.png"), tr("Note"), new FormNote(stack));
	m_pWidgetGroup->AddChild(tr("./images/user.png"), tr("Contact"), new FormContact(stack));
	m_pWidgetGroup->AddChild(tr("./images/money.png"), tr("Account"), new FormAccount(stack));
	m_pWidgetGroup->AddChild(tr("./images/task.png"), tr("Task"), new FormAccount(stack));
	m_pWidgetGroup->AddChild(tr("./images/mail.png"), tr("E-Mail"), new FormAccount(stack));
	m_pWidgetGroup->AddChild(tr("./images/transfer.png"), tr("Ftp"), new FormAccount(stack));
	m_pWidgetGroup->AddChild(tr("./images/download.png"), tr("Download"), new FormAccount(stack));
	m_pWidgetGroup->AddChild(tr("./images/music.png"), tr("Music"), new FormAccount(stack));
	m_pWidgetGroup->AddChild(tr("./images/setting.png"), tr("Setting"), new FormAccount(stack));
	m_pWidgetGroup->SetCurrentWidget(wid);
}

void MainFrame::createAction(void)
{
	///创建Action
	QAction *actNew = new QAction(QIcon("./images/filenew.png"),tr("New(&N)"), this);
    actNew->setShortcut(tr("Ctrl+N"));
	connect(actNew, SIGNAL(triggered()), this, SLOT(fileNew()));
	QAction *actOpen = new QAction(QIcon("./images/fileopen.png"),tr("Open(&O)"), this);
    actOpen->setShortcut(tr("Ctrl+O"));
	connect(actOpen, SIGNAL(triggered()), this, SLOT(open()));
	QAction *actSave = new QAction(QIcon("./images/filesave.png"),tr("Save(&S)"), this);
    actSave->setShortcut(tr("Ctrl+S"));
	connect(actSave, SIGNAL(triggered()), this, SLOT(save()));

	QAction *actExit = new QAction(QIcon("./images/exit.png"),tr("Exit(&X)"), this);
    actExit->setShortcut(tr("Ctrl+X"));
	QAction *actUndo = new QAction(QIcon("./images/undo.png"),tr("Undo(&U)"), this);
    actUndo->setShortcut(tr("Ctrl+U"));
	QAction *actRedo = new QAction(QIcon("./images/redo.png"),tr("Redo(&R)"), this);
    actRedo->setShortcut(tr("Ctrl+R"));
	QAction *actCut = new QAction(QIcon("./images/cut.png"),tr("Cut(&T)"), this);
    actCut->setShortcut(tr("Ctrl+T"));
	QAction *actCopy = new QAction(QIcon("./images/copy.png"),tr("Copy(&C)"), this);
    actCopy->setShortcut(tr("Ctrl+C"));
	QAction *actPaste = new QAction(QIcon("./images/paste.png"),tr("Past(&P)"), this);
    actPaste->setShortcut(tr("Ctrl+P"));
	QAction *actFind = new QAction(QIcon("./images/find.png"),tr("Find(&F)"), this);
    actFind->setShortcut(tr("Ctrl+F"));
	QAction *actSet = new QAction(QIcon("./images/set.png"),tr("Setting(&S)"), this);
    actSet->setShortcut(tr("Ctrl+Z"));
	QAction *actAbout = new QAction(QIcon("./images/about.png"),tr("About(&A)"), this);
    actAbout->setShortcut(tr("Ctrl+A"));
	connect(actAbout, SIGNAL(triggered()), this, SLOT(onAbout()));
	QAction *actAboutQt = new QAction(QIcon("./images/qt.png"),tr("About Qt(&A)"), this);
    actAbout->setShortcut(tr("Ctrl+A"));
	connect(actAboutQt, SIGNAL(triggered()), this, SLOT(onAboutQt()));

	QAction *actAdd = new QAction(QIcon(":/images/add.png"),tr("Add(&D)"), this);
    actFind->setShortcut(tr("Ctrl+D"));
	QAction *actRemove = new QAction(QIcon("./images/remove.png"),tr("Remove(&R)"), this);
    actSet->setShortcut(tr("Ctrl+R"));
	QAction *actDelete = new QAction(QIcon("./images/delete.png"),tr("Delete(&L)"), this);
    actAbout->setShortcut(tr("Ctrl+L"));
	connect(actDelete, SIGNAL(triggered()), this, SLOT(fileDelete()));
	///创建Menu
	//menuBar()->setFont(QFont(tr("宋体"), 9));
	QMenu *fileMenu = menuBar()->addMenu(tr("File(&F)"));
	//fileMenu->setFont(QFont(tr("宋体"), 9));
	fileMenu->addAction(actNew);
	fileMenu->addAction(actOpen);
	fileMenu->addAction(actSave);
	fileMenu->addSeparator();
	fileMenu->addAction(actExit);
	QMenu *editMenu = menuBar()->addMenu(tr("Edit(&E)"));
	//editMenu->setFont(QFont(tr("宋体"), 9));
	editMenu->addAction(actUndo);
	editMenu->addAction(actRedo);
	editMenu->addSeparator();
	editMenu->addAction(actCut);
	editMenu->addAction(actCopy);
	editMenu->addAction(actPaste);
	editMenu->addSeparator();
	editMenu->addAction(actFind);
	QMenu *viewMenu = menuBar()->addMenu(tr("Find(&V)"));
	//viewMenu->setFont(QFont(tr("宋体"), 9));
	QMenu *toolMenu = menuBar()->addMenu(tr("Tools(&T)"));
	//toolMenu->setFont(QFont(tr("宋体"), 9));
	toolMenu->addAction(actSet);
	QMenu *helpMenu = menuBar()->addMenu(tr("Help(&H)"));
	//helpMenu->setFont(QFont(tr("宋体"), 9));
	helpMenu->addAction(actAbout);
	helpMenu->addAction(actAboutQt);
	QMenu *styleMenu = menuBar()->addMenu("Themes(&S)" );
	//styleMenu->setFont(QFont(tr("宋体"), 9));
	QActionGroup *actGrp = new QActionGroup(this);	
	QStringList list = QStyleFactory::keys();
	for(int i = 0; i < list.size(); i++)
	{
		QAction* action =styleMenu->addAction(list[i]);
		actGrp->addAction(action);	
	}
	connect(actGrp, SIGNAL(triggered(QAction* )), this, SLOT(styleClicked(QAction* )));
	///创建Toolbar
	QToolBar *toolBar = addToolBar(tr("File"));
	toolBar->setIconSize(QSize(24, 24));

	toolBar->addAction(actNew);
	toolBar->addAction(actOpen);
	toolBar->addAction(actSave);
	toolBar->addSeparator();
	toolBar->addAction(actUndo);
	toolBar->addAction(actRedo);
	toolBar->addAction(actCut);
	toolBar->addAction(actCopy);
	toolBar->addAction(actPaste);
	toolBar->addSeparator();
	toolBar->addAction(actFind);
	toolBar->addAction(actSet);
	toolBar->addSeparator();
	toolBar->addAction(actAbout);
	QToolBar *editBar = addToolBar(tr("edit"));
	editBar->setIconSize(QSize(24, 24));
	editBar->addAction(actDelete);
	editBar->addAction(actAdd);
	editBar->addAction(actRemove);

	//toolBar->setHidden(true);

	setStyle(tr("CleanLooks"));
}

void MainFrame::setStyle(QString& style)
{
	static QPalette pl = qApp->palette();
    QStyle *s = QStyleFactory::create( style );
    if ( s )
	{
		QApplication::setStyle( s );
	}

	if(style == tr("CDE"))
	{
		QPalette p( QColor( 75, 123, 130 ) );
		p.setColor( QPalette::Active, QPalette::Base, QColor( 55, 77, 78 ) );
		p.setColor( QPalette::Inactive, QPalette::Base, QColor( 55, 77, 78 ) );
		p.setColor( QPalette::Disabled, QPalette::Base, QColor( 55, 77, 78 ) );
		p.setColor( QPalette::Active, QPalette::Highlight, Qt::white );
		p.setColor( QPalette::Active, QPalette::HighlightedText, QColor( 55, 77, 78 ) );
		p.setColor( QPalette::Inactive, QPalette::Highlight, Qt::white );
		p.setColor( QPalette::Inactive, QPalette::HighlightedText, QColor( 55, 77, 78 ) );
		p.setColor( QPalette::Disabled, QPalette::Highlight, Qt::white );
		p.setColor( QPalette::Disabled, QPalette::HighlightedText, QColor( 55, 77, 78 ) );
		p.setColor( QPalette::Active, QPalette::Foreground, Qt::white );
		p.setColor( QPalette::Active, QPalette::Text, Qt::white );
		p.setColor( QPalette::Active, QPalette::ButtonText, Qt::white );
		p.setColor( QPalette::Inactive, QPalette::Foreground, Qt::white );
		p.setColor( QPalette::Inactive, QPalette::Text, Qt::white );
		p.setColor( QPalette::Inactive, QPalette::ButtonText, Qt::white );
		p.setColor( QPalette::Disabled, QPalette::Foreground, Qt::lightGray );
		p.setColor( QPalette::Disabled, QPalette::Text, Qt::lightGray );
		p.setColor( QPalette::Disabled, QPalette::ButtonText, Qt::lightGray );
		qApp->setPalette( p );
	}
	else
	{
		qApp->setPalette( pl );
	}
}

void MainFrame::styleClicked(QAction* action)
{
	QString str = action->text();
	setStyle(action->text());
}

void MainFrame::onAbout(void)
{
	QMessageBox::about(this, tr("About hello"),
		tr("hello 0.1  copyright Hongjun Li " ) + tr(__TIMESTAMP__));

	//TestDialog dlg;
	//dlg.exec();
}
void MainFrame::onAboutQt()
{
	QApplication::aboutQt();
}

void MainFrame::save()
{
	m_pWidgetGroup->CurrentWidget()->save();
}

void MainFrame::fileNew()
{
	m_pWidgetGroup->CurrentWidget()->fileNew();
}

void MainFrame::open(void)
{
	m_pWidgetGroup->CurrentWidget()->open();
}

void MainFrame::fileDelete(void)
{
	m_pWidgetGroup->CurrentWidget()->fileDelete();
}
